package com.ycg.store.filter;

import cn.hutool.core.util.StrUtil;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author YCG
 * @date 2024-05-16 14:53
 * @description 只有特定请求头才可以进入系统
 */
@Component
@Slf4j
public class CredentialFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String credential = request.getHeader("Credential");
        String method = request.getMethod();
        log.debug("进入 CredentialFilter");
//        if (StrUtil.equals(method,"OPTIONS")) {
//            // 跳过options请求
//            filterChain.doFilter(request,response);
//            return;
//        } else if (StrUtil.isBlank(credential)) {
//            System.out.println("credential错误");
//            // 不是options请求且credential为空, 直接返回
//            response.setStatus(400);
//            PrintWriter writer = response.getWriter();
//            writer.write("bad request");
//            writer.flush();
//            return;
//        }
        // 正常请求, 放行
        filterChain.doFilter(request,response);
    }
}
